<?php
/**
 * Classe singleton di utilità per la gestione della connessione alla base dati e delle query.
 *
 * @author Damiano Ciarla, Fabrizio Filieri
 * @version 1.0
 * @category lib/database
 */
class DB_PDO implements Database
{
	private $connectDB;
	private $statement;
	private $query;

	public function connect($config)
	{
		switch ($config::$driver)
		{
			case "pgsql":
				try
				{
					$dsn = "pgsql" . ":dbname=" . $config::$schema . ";host=" . $config::$host;
					$dbPDO = new PDO($dsn, $config::$user, $config::$password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
					$dbPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
					$this->connectDB = $dbPDO;
				}
				catch(PDOException $e)
				{
					BXD::newException("DatabaseConnectionError", $e->getMessage());
				};
				break;

			case "mysql":
				try
				{
					$dsn = "mysql" . ":host=" . $config::$host . ";dbname=" . $config::$schema;
					$dbPDO = new PDO($dsn, $config::$user, $config::$password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
					$dbPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
					$this->connectDB = $dbPDO;
				}
				catch(PDOException $e)
				{
					BXD::newException("DatabaseConnectionError", $e->getMessage());
				};
				break;
		}
	}

	public function close()
	{
		$this->connectDB = null;
	}

	public function beginTransaction()
	{
		$this->connectDB->beginTransaction();
	}

	public function commit()
	{
		$this->connectDB->commit();
	}

	public function rollback()
	{
		$this->connectDB->rollBack();
	}

	public function prepareQuery($query)
	{
		try
		{
			$this->query = $query;

			$this->statement = $this->connectDB->prepare($query);

			if ($this->statement === FALSE) return FALSE;

			return TRUE;
		}
		catch(PDOException $e)
		{
			echo $e->getMessage();

			return FALSE;
		};
	}

	public function bindParamInt($param, $value)
	{
		$this->statement->bindParam($param, $value, PDO::PARAM_INT);
	}

	public function bindParamString($param, $value)
	{
		$this->statement->bindParam($param, $value, PDO::PARAM_STR);
	}

	public function bindParamBoolean($param, $value)
	{
		$this->statement->bindParam($param, $value, PDO::PARAM_BOOL);
	}

	public function bindParamGeneric($param, $value)
	{
		$this->statement->bindParam($param, $value);
	}

	public function execute()
	{
		try
		{
			return $this->statement->execute();
		}
		catch(PDOException $e)
		{
			return FALSE;
		}
	}

	public function exec($query)
	{
		try
		{
			return $this->connectDB->exec($query);
		}
		catch(PDOException $e)
		{
			return FALSE;
		}
	}

	public function query($query)
	{
		try
		{
			return $this->connectDB->query($query);
		}
		catch(PDOException $e)
		{
			return FALSE;
		}
	}

	public function getLastError()
	{
		return $this->statement->errorInfo();
	}

	public function getResult()
	{
		return $this->statement;
	}

	public function getNumRows()
	{
		return $this->statement->rowCount();
	}

	public function getLastInsertId()
	{
		return $this->connectDB->lastInsertId();
	}

	public function getArrayAssocResult()
	{
		return $this->statement->fetch(PDO::FETCH_ASSOC);
	}

	public function getFetchAll()
	{
		return $this->statement->fetchAll(PDO::FETCH_ASSOC);
	}

}
?>